home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / cbibcode.arc / RANDOM.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-05  |  657 b   |  33 lines

  1.  /* random.c from page 237*/
  2.  #include <stdio.h>
  3.  #include <stdlib.h>
  4.  #include <time.h>            /* randomize() uses time()*/
  5.  main()
  6. {       int seq[20], count = 0, i, randnum;
  7.  
  8.                     /* initialize the random number generator*/
  9.  
  10. randomize();
  11. while(count < 20)
  12. {
  13.     randnum = random(20) + 1;
  14.                     /*is the number already present in seq[] */
  15.     for(i = 0; i < count; i++)
  16.     {
  17.         if (randnum == seq[i]) break;
  18.     }
  19.     if( i >= count)          /* Not in seq[] */
  20.     {
  21.         seq[count] = randnum;
  22.         count++;
  23.     }
  24. }
  25.                     /* Print the random sequence */
  26. printf("Random sequence (1-20) = \n");
  27. for(i = 0; i < count; i++)
  28. printf ("%d", seq[i]);
  29. printf("\n");
  30. }
  31.  
  32.  
  33.